<?php
namespace app\admin\controller;
use app\admin\controller\BaseController;
use app\common\errcode\AdminErrCode;
use app\common\errcode\AdminErrMsg;
use think\Loader;

class UserController extends BaseController
{
    protected $userModel;

    public function __construct()
    {
        parent::__construct();
        $this->userModel = new \app\admin\model\UserModel();
    }

    /**
     * 创建用户
     * @param string $username   用户名
     * @param string $email      邮箱
     * @param string $password   密码
     * @param int    $type       用户类型 管理员:1 普通用户:2
     * @param string $remark     备注
     * @return string
     */
    public function create()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('User');
                $check = $validate->scene('add')->check($data);
                if(!$check){
                    return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                if($data['type'] == 1){
                    // if($this->userModel->findOneByName(trim($data['username']))) return $this->jsonJump(AdminErrCode::NAME_EXIST,AdminErrMsg::NAME_EXIST);     
                    if($this->userModel->findOneByEmail(trim($data['email']))) return $this->jsonJump(AdminErrCode::EMAIL_EXIST,AdminErrMsg::EMAIL_EXIST);
                    if($this->userModel->insertOne($data)) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                    return $this->jsonJump(AdminErrCode::ADD_USER_FAIL,AdminErrMsg::ADD_USER_FAIL);
                }else{
                    // if($this->userModel->findOneByNameApi(trim($data['username']))) return $this->jsonJump(AdminErrCode::NAME_EXIST,AdminErrMsg::NAME_EXIST);     
                    if($this->userModel->findOneByEmailApi(trim($data['email']))) return $this->jsonJump(AdminErrCode::EMAIL_EXIST,AdminErrMsg::EMAIL_EXIST);
                    if($this->userModel->insertOne($data)) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                    return $this->jsonJump(AdminErrCode::ADD_USER_FAIL,AdminErrMsg::ADD_USER_FAIL);    
                }
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 删除
     * @param int $uid
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('User');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());  
                $userinfo = $this->userModel->findOneById((int)$data['uid']);
                if(empty($userinfo)) return $this->jsonJump(AdminErrCode::DELETE_ADMIN_FAIL,AdminErrMsg::DELETE_ADMIN_FAIL);
                if(!empty($userinfo['super']) && $userinfo['super'] == 1) return $this->jsonJump(AdminErrCode::SUPER_DELETE,AdminErrMsg::SUPER_DELETE);    
                if($this->userModel->deleteOne((int)$data['uid'],$this->uid) === true) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                return $this->jsonJump(AdminErrCode::DELETE_ADMIN_FAIL,AdminErrMsg::DELETE_ADMIN_FAIL);
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 用户编辑
     * @param   string  $method     方法:编辑文生图次数:editPictureNum
     * @param   int     $pictureNum 文生图次数  
     * @param   int     $userid     用户userid,字符串形式的 SS2023030001  
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('User');
                $check = $validate->scene('editBefore')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());  
                switch ($data['method']) {
                    case 'editPictureNum':
                        $check = $validate->scene('editPictureNum')->check($data);
                        if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                        $res = $this->userModel->edit_picture_num($data['userid'],$data['pictureNum']);
                        break;
                    default:
                        return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR);
                        break;
                }
                if($res !== false){
                    return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                }
                return $this->jsonJump(AdminErrCode::EDIT_USER_PICNUM_FAIL,AdminErrMsg::EDIT_USER_PICNUM_FAIL);
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 用户列表
     * @param   string  $company    公司     
     * @param   string  $username   用户姓名
     * @param   int     $pagesize   页码大小
     * @param   int     $page       页码
     * @return  string
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS,$this->userModel->list($data));
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

}